package linePainter;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.text.JTextComponent;

public class LinePainter {

    private Stroke normalStroke = new BasicStroke(1);
    private Stroke thickStroke = new BasicStroke(1);
    private Color normalColor = Color.lightGray;
    private Color tickColor = Color.cyan.darker();
    private Color decimalColor = Color.red;

    public void paint(JComponent component, Graphics2D g, Color lineColor) {
        g.setColor(Color.gray);
        g.setFont(component.getFont());
        Rectangle2D bounds = g.getFontMetrics().getStringBounds("0", g);
        int unitWidth = (int) bounds.getWidth();

        int x = component.getWidth();
        int rightGap = 0;
        if (component.getInsets() != null) {
            rightGap = component.getInsets().right;
        }
        if (component.getBorder() != null) {
            rightGap = rightGap + component.getBorder().getBorderInsets(component).right;
        }
        if (component instanceof JTextComponent) {
            JTextComponent textComponent = (JTextComponent) component;
            Insets insets = textComponent.getMargin();
            rightGap = rightGap + insets.right;
            rightGap = rightGap + 2;
        }

        x = x - rightGap;

        int index = 0;
        while (x > unitWidth * 2) {
            x = x - unitWidth;
            if (index == 1) {
                g.setStroke(thickStroke);
                g.setColor(decimalColor);
            } else {
                if ((index - 1) % 3 == 0) {
                    g.setColor(tickColor);
                } else {
                    g.setColor(normalColor);
                }
                g.setStroke(normalStroke);
            }
            g.drawLine(x, 0, x, component.getHeight());
            index++;
        }
        if (lineColor != null) {
            g.setColor(lineColor);
            g.setStroke(this.normalStroke);
            g.drawLine(0, component.getHeight() - 1, component.getWidth(), component.getHeight() - 1);
        }
    }
}
